﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using MessageBox = System.Windows.MessageBox;
using UserControl = System.Windows.Controls.UserControl;
using static CosAging.PublicClass;
using System.Globalization;
using Timer = System.Timers.Timer;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO.Ports;

namespace CosAging.View
{
    /// <summary>
    /// SystemSetting.xaml 的交互逻辑
    /// </summary>
    public partial class SystemSetting : UserControl
    {
        设置参数 参数 = new 设置参数();
        public SystemSetting()
        {
            InitializeComponent();

;       }

        private void 读取(object sender, RoutedEventArgs e)
        {
            myIniFile.iniFileName = Apppath + INISYS;
            
            参数.阶段停留时长1 = myIniFile.ReadString("Setting", "阶段停留时长1", 99);
            参数.阶段停留时长2 = myIniFile.ReadString("Setting", "阶段停留时长2", 99);
            参数.阶段停留时长3 = myIniFile.ReadString("Setting", "阶段停留时长3", 99);
            参数.阶段停留时长4 = myIniFile.ReadString("Setting", "阶段停留时长4", 99);
            参数.阶段停留时长5 = myIniFile.ReadString("Setting", "阶段停留时长5", 99);
            参数.阶段停留时长6 = myIniFile.ReadString("Setting", "阶段停留时长6", 99);
            参数.阶段停留时长7 = myIniFile.ReadString("Setting", "阶段停留时长7", 99);
            参数.阶段停留时长8 = myIniFile.ReadString("Setting", "阶段停留时长8", 99);
            参数.阶段停留时长9 = myIniFile.ReadString("Setting", "阶段停留时长9", 99);
            参数.阶段停留时长10 = myIniFile.ReadString("Setting", "阶段停留时长10", 99);
            阶段停留时长1.DataContext = 参数;
            阶段停留时长2.DataContext = 参数;
            阶段停留时长3.DataContext = 参数;
            阶段停留时长4.DataContext = 参数;
            阶段停留时长5.DataContext = 参数;
            阶段停留时长6.DataContext = 参数;
            阶段停留时长7.DataContext = 参数;
            阶段停留时长8.DataContext = 参数;
            阶段停留时长9.DataContext = 参数;
            阶段停留时长10.DataContext = 参数;

            参数.阶段电流1 = myIniFile.ReadString("Setting", "阶段电流1", 99);
            参数.阶段电流2 = myIniFile.ReadString("Setting", "阶段电流2", 99);
            参数.阶段电流3 = myIniFile.ReadString("Setting", "阶段电流3", 99);
            参数.阶段电流4 = myIniFile.ReadString("Setting", "阶段电流4", 99);
            参数.阶段电流5 = myIniFile.ReadString("Setting", "阶段电流5", 99);
            参数.阶段电流6 = myIniFile.ReadString("Setting", "阶段电流6", 99);
            参数.阶段电流7 = myIniFile.ReadString("Setting", "阶段电流7", 99);
            参数.阶段电流8 = myIniFile.ReadString("Setting", "阶段电流8", 99);
            参数.阶段电流9 = myIniFile.ReadString("Setting", "阶段电流9", 99);
            参数.阶段电流10 = myIniFile.ReadString("Setting", "阶段电流10", 99);
            阶段电流1.DataContext = 参数;
            阶段电流2.DataContext = 参数;
            阶段电流3.DataContext = 参数;
            阶段电流4.DataContext = 参数;
            阶段电流5.DataContext = 参数;
            阶段电流6.DataContext = 参数;
            阶段电流7.DataContext = 参数;
            阶段电流8.DataContext = 参数;
            阶段电流9.DataContext = 参数;
            阶段电流10.DataContext = 参数;

            参数.有效阶段数 = myIniFile.ReadString("Setting", "有效阶段数", 99);
            有效阶段数.DataContext = 参数;
        }

        private void 写入(object sender, RoutedEventArgs e)
        {

            参数.阶段停留时长1 = 阶段停留时长1.Text;
            参数.阶段停留时长2 = 阶段停留时长2.Text;
            参数.阶段停留时长3 = 阶段停留时长3.Text;
            参数.阶段停留时长4 = 阶段停留时长4.Text;
            参数.阶段停留时长5 = 阶段停留时长5.Text;
            参数.阶段停留时长6 = 阶段停留时长6.Text;
            参数.阶段停留时长7 = 阶段停留时长7.Text;
            参数.阶段停留时长8 = 阶段停留时长8.Text;
            参数.阶段停留时长9 = 阶段停留时长9.Text;
            参数.阶段停留时长10 = 阶段停留时长10.Text;

            参数.阶段电流1 = 阶段电流1.Text;
            参数.阶段电流2 = 阶段电流2.Text;
            参数.阶段电流3 = 阶段电流3.Text;
            参数.阶段电流4 = 阶段电流4.Text;
            参数.阶段电流5 = 阶段电流5.Text;
            参数.阶段电流6 = 阶段电流6.Text;
            参数.阶段电流7 = 阶段电流7.Text;
            参数.阶段电流8 = 阶段电流8.Text;
            参数.阶段电流9 = 阶段电流9.Text;
            参数.阶段电流10 = 阶段电流10.Text;

            参数.有效阶段数 = 有效阶段数.Text;

            switch (参数.有效阶段数)
            {
                case "1":
                    myIniFile.WriteString("Setting", "临时阶段电流1", 参数.阶段电流1);
                    myIniFile.WriteString("Setting", "临时阶段电流2", "0");
                    myIniFile.WriteString("Setting", "临时阶段电流3", "0");
                    myIniFile.WriteString("Setting", "临时阶段电流4", "0");
                    myIniFile.WriteString("Setting", "临时阶段电流5", "0");
                    myIniFile.WriteString("Setting", "临时阶段电流6", "0");
                    myIniFile.WriteString("Setting", "临时阶段电流7", "0");
                    myIniFile.WriteString("Setting", "临时阶段电流8", "0");
                    myIniFile.WriteString("Setting", "临时阶段电流9", "0");
                    myIniFile.WriteString("Setting", "临时阶段电流10", "0");

                    myIniFile.WriteString("Setting", "临时阶段停留时长1", 参数.阶段停留时长1);
                    myIniFile.WriteString("Setting", "临时阶段停留时长2", "0");
                    myIniFile.WriteString("Setting", "临时阶段停留时长3", "0");
                    myIniFile.WriteString("Setting", "临时阶段停留时长4", "0");
                    myIniFile.WriteString("Setting", "临时阶段停留时长5", "0");
                    myIniFile.WriteString("Setting", "临时阶段停留时长6", "0");
                    myIniFile.WriteString("Setting", "临时阶段停留时长7", "0");
                    myIniFile.WriteString("Setting", "临时阶段停留时长8", "0");
                    myIniFile.WriteString("Setting", "临时阶段停留时长9", "0");
                    myIniFile.WriteString("Setting", "临时阶段停留时长10", "0");
                    break;
                case "2":
                    myIniFile.WriteString("Setting", "临时阶段电流1", 参数.阶段电流1);
                    myIniFile.WriteString("Setting", "临时阶段电流2", 参数.阶段电流2);
                    myIniFile.WriteString("Setting", "临时阶段电流3", "0");
                    myIniFile.WriteString("Setting", "临时阶段电流4", "0");
                    myIniFile.WriteString("Setting", "临时阶段电流5", "0");
                    myIniFile.WriteString("Setting", "临时阶段电流6", "0");
                    myIniFile.WriteString("Setting", "临时阶段电流7", "0");
                    myIniFile.WriteString("Setting", "临时阶段电流8", "0");
                    myIniFile.WriteString("Setting", "临时阶段电流9", "0");
                    myIniFile.WriteString("Setting", "临时阶段电流10", "0");

                    myIniFile.WriteString("Setting", "临时阶段停留时长1", 参数.阶段停留时长1);
                    myIniFile.WriteString("Setting", "临时阶段停留时长2", 参数.阶段停留时长2);
                    myIniFile.WriteString("Setting", "临时阶段停留时长3", "0");
                    myIniFile.WriteString("Setting", "临时阶段停留时长4", "0");
                    myIniFile.WriteString("Setting", "临时阶段停留时长5", "0");
                    myIniFile.WriteString("Setting", "临时阶段停留时长6", "0");
                    myIniFile.WriteString("Setting", "临时阶段停留时长7", "0");
                    myIniFile.WriteString("Setting", "临时阶段停留时长8", "0");
                    myIniFile.WriteString("Setting", "临时阶段停留时长9", "0");
                    myIniFile.WriteString("Setting", "临时阶段停留时长10", "0");
                    break;
                case "3":
                    myIniFile.WriteString("Setting", "临时阶段电流1", 参数.阶段电流1);
                    myIniFile.WriteString("Setting", "临时阶段电流2", 参数.阶段电流2);
                    myIniFile.WriteString("Setting", "临时阶段电流3", 参数.阶段电流3);
                    myIniFile.WriteString("Setting", "临时阶段电流4", "0");
                    myIniFile.WriteString("Setting", "临时阶段电流5", "0");
                    myIniFile.WriteString("Setting", "临时阶段电流6", "0");
                    myIniFile.WriteString("Setting", "临时阶段电流7", "0");
                    myIniFile.WriteString("Setting", "临时阶段电流8", "0");
                    myIniFile.WriteString("Setting", "临时阶段电流9", "0");
                    myIniFile.WriteString("Setting", "临时阶段电流10", "0");

                    myIniFile.WriteString("Setting", "临时阶段停留时长1", 参数.阶段停留时长1);
                    myIniFile.WriteString("Setting", "临时阶段停留时长2", 参数.阶段停留时长2);
                    myIniFile.WriteString("Setting", "临时阶段停留时长3", 参数.阶段停留时长3);
                    myIniFile.WriteString("Setting", "临时阶段停留时长4", "0");
                    myIniFile.WriteString("Setting", "临时阶段停留时长5", "0");
                    myIniFile.WriteString("Setting", "临时阶段停留时长6", "0");
                    myIniFile.WriteString("Setting", "临时阶段停留时长7", "0");
                    myIniFile.WriteString("Setting", "临时阶段停留时长8", "0");
                    myIniFile.WriteString("Setting", "临时阶段停留时长9", "0");
                    myIniFile.WriteString("Setting", "临时阶段停留时长10", "0");
                    break;
                case "4":
                    myIniFile.WriteString("Setting", "临时阶段电流1", 参数.阶段电流1);
                    myIniFile.WriteString("Setting", "临时阶段电流2", 参数.阶段电流2);
                    myIniFile.WriteString("Setting", "临时阶段电流3", 参数.阶段电流3);
                    myIniFile.WriteString("Setting", "临时阶段电流4", 参数.阶段电流4);
                    myIniFile.WriteString("Setting", "临时阶段电流5", "0");
                    myIniFile.WriteString("Setting", "临时阶段电流6", "0");
                    myIniFile.WriteString("Setting", "临时阶段电流7", "0");
                    myIniFile.WriteString("Setting", "临时阶段电流8", "0");
                    myIniFile.WriteString("Setting", "临时阶段电流9", "0");
                    myIniFile.WriteString("Setting", "临时阶段电流10", "0");

                    myIniFile.WriteString("Setting", "临时阶段停留时长1", 参数.阶段停留时长1);
                    myIniFile.WriteString("Setting", "临时阶段停留时长2", 参数.阶段停留时长2);
                    myIniFile.WriteString("Setting", "临时阶段停留时长3", 参数.阶段停留时长3);
                    myIniFile.WriteString("Setting", "临时阶段停留时长4", 参数.阶段停留时长4);
                    myIniFile.WriteString("Setting", "临时阶段停留时长5", "0");
                    myIniFile.WriteString("Setting", "临时阶段停留时长6", "0");
                    myIniFile.WriteString("Setting", "临时阶段停留时长7", "0");
                    myIniFile.WriteString("Setting", "临时阶段停留时长8", "0");
                    myIniFile.WriteString("Setting", "临时阶段停留时长9", "0");
                    myIniFile.WriteString("Setting", "临时阶段停留时长10", "0");
                    break;
                case "5":
                    myIniFile.WriteString("Setting", "临时阶段电流1", 参数.阶段电流1);
                    myIniFile.WriteString("Setting", "临时阶段电流2", 参数.阶段电流2);
                    myIniFile.WriteString("Setting", "临时阶段电流3", 参数.阶段电流3);
                    myIniFile.WriteString("Setting", "临时阶段电流4", 参数.阶段电流4);
                    myIniFile.WriteString("Setting", "临时阶段电流5", 参数.阶段电流5);
                    myIniFile.WriteString("Setting", "临时阶段电流6", "0");
                    myIniFile.WriteString("Setting", "临时阶段电流7", "0");
                    myIniFile.WriteString("Setting", "临时阶段电流8", "0");
                    myIniFile.WriteString("Setting", "临时阶段电流9", "0");
                    myIniFile.WriteString("Setting", "临时阶段电流10", "0");

                    myIniFile.WriteString("Setting", "临时阶段停留时长1", 参数.阶段停留时长1);
                    myIniFile.WriteString("Setting", "临时阶段停留时长2", 参数.阶段停留时长2);
                    myIniFile.WriteString("Setting", "临时阶段停留时长3", 参数.阶段停留时长3);
                    myIniFile.WriteString("Setting", "临时阶段停留时长4", 参数.阶段停留时长4);
                    myIniFile.WriteString("Setting", "临时阶段停留时长5", 参数.阶段停留时长5);
                    myIniFile.WriteString("Setting", "临时阶段停留时长6", "0");
                    myIniFile.WriteString("Setting", "临时阶段停留时长7", "0");
                    myIniFile.WriteString("Setting", "临时阶段停留时长8", "0");
                    myIniFile.WriteString("Setting", "临时阶段停留时长9", "0");
                    myIniFile.WriteString("Setting", "临时阶段停留时长10", "0");
                    break;
                case "6":
                    myIniFile.WriteString("Setting", "临时阶段电流1", 参数.阶段电流1);
                    myIniFile.WriteString("Setting", "临时阶段电流2", 参数.阶段电流2);
                    myIniFile.WriteString("Setting", "临时阶段电流3", 参数.阶段电流3);
                    myIniFile.WriteString("Setting", "临时阶段电流4", 参数.阶段电流4);
                    myIniFile.WriteString("Setting", "临时阶段电流5", 参数.阶段电流5);
                    myIniFile.WriteString("Setting", "临时阶段电流6", 参数.阶段电流6);
                    myIniFile.WriteString("Setting", "临时阶段电流7", "0");
                    myIniFile.WriteString("Setting", "临时阶段电流8", "0");
                    myIniFile.WriteString("Setting", "临时阶段电流9", "0");
                    myIniFile.WriteString("Setting", "临时阶段电流10", "0");

                    myIniFile.WriteString("Setting", "临时阶段停留时长1", 参数.阶段停留时长1);
                    myIniFile.WriteString("Setting", "临时阶段停留时长2", 参数.阶段停留时长2);
                    myIniFile.WriteString("Setting", "临时阶段停留时长3", 参数.阶段停留时长3);
                    myIniFile.WriteString("Setting", "临时阶段停留时长4", 参数.阶段停留时长4);
                    myIniFile.WriteString("Setting", "临时阶段停留时长5", 参数.阶段停留时长5);
                    myIniFile.WriteString("Setting", "临时阶段停留时长6", 参数.阶段停留时长6);
                    myIniFile.WriteString("Setting", "临时阶段停留时长7", "0");
                    myIniFile.WriteString("Setting", "临时阶段停留时长8", "0");
                    myIniFile.WriteString("Setting", "临时阶段停留时长9", "0");
                    myIniFile.WriteString("Setting", "临时阶段停留时长10", "0");
                    break;
                case "7":
                    myIniFile.WriteString("Setting", "临时阶段电流1", 参数.阶段电流1);
                    myIniFile.WriteString("Setting", "临时阶段电流2", 参数.阶段电流2);
                    myIniFile.WriteString("Setting", "临时阶段电流3", 参数.阶段电流3);
                    myIniFile.WriteString("Setting", "临时阶段电流4", 参数.阶段电流4);
                    myIniFile.WriteString("Setting", "临时阶段电流5", 参数.阶段电流5);
                    myIniFile.WriteString("Setting", "临时阶段电流6", 参数.阶段电流6);
                    myIniFile.WriteString("Setting", "临时阶段电流7", 参数.阶段电流7);
                    myIniFile.WriteString("Setting", "临时阶段电流8", "0");
                    myIniFile.WriteString("Setting", "临时阶段电流9", "0");
                    myIniFile.WriteString("Setting", "临时阶段电流10", "0");

                    myIniFile.WriteString("Setting", "临时阶段停留时长1", 参数.阶段停留时长1);
                    myIniFile.WriteString("Setting", "临时阶段停留时长2", 参数.阶段停留时长2);
                    myIniFile.WriteString("Setting", "临时阶段停留时长3", 参数.阶段停留时长3);
                    myIniFile.WriteString("Setting", "临时阶段停留时长4", 参数.阶段停留时长4);
                    myIniFile.WriteString("Setting", "临时阶段停留时长5", 参数.阶段停留时长5);
                    myIniFile.WriteString("Setting", "临时阶段停留时长6", 参数.阶段停留时长6);
                    myIniFile.WriteString("Setting", "临时阶段停留时长7", 参数.阶段停留时长7);
                    myIniFile.WriteString("Setting", "临时阶段停留时长8", "0");
                    myIniFile.WriteString("Setting", "临时阶段停留时长9", "0");
                    myIniFile.WriteString("Setting", "临时阶段停留时长10", "0");
                    break;
                case "8":
                    myIniFile.WriteString("Setting", "临时阶段电流1", 参数.阶段电流1);
                    myIniFile.WriteString("Setting", "临时阶段电流2", 参数.阶段电流2);
                    myIniFile.WriteString("Setting", "临时阶段电流3", 参数.阶段电流3);
                    myIniFile.WriteString("Setting", "临时阶段电流4", 参数.阶段电流4);
                    myIniFile.WriteString("Setting", "临时阶段电流5", 参数.阶段电流5);
                    myIniFile.WriteString("Setting", "临时阶段电流6", 参数.阶段电流6);
                    myIniFile.WriteString("Setting", "临时阶段电流7", 参数.阶段电流7);
                    myIniFile.WriteString("Setting", "临时阶段电流8", 参数.阶段电流8);
                    myIniFile.WriteString("Setting", "临时阶段电流9", "0");
                    myIniFile.WriteString("Setting", "临时阶段电流10", "0");

                    myIniFile.WriteString("Setting", "临时阶段停留时长1", 参数.阶段停留时长1);
                    myIniFile.WriteString("Setting", "临时阶段停留时长2", 参数.阶段停留时长2);
                    myIniFile.WriteString("Setting", "临时阶段停留时长3", 参数.阶段停留时长3);
                    myIniFile.WriteString("Setting", "临时阶段停留时长4", 参数.阶段停留时长4);
                    myIniFile.WriteString("Setting", "临时阶段停留时长5", 参数.阶段停留时长5);
                    myIniFile.WriteString("Setting", "临时阶段停留时长6", 参数.阶段停留时长6);
                    myIniFile.WriteString("Setting", "临时阶段停留时长7", 参数.阶段停留时长7);
                    myIniFile.WriteString("Setting", "临时阶段停留时长8", 参数.阶段停留时长8);
                    myIniFile.WriteString("Setting", "临时阶段停留时长9", "0");
                    myIniFile.WriteString("Setting", "临时阶段停留时长10", "0");
                    break;
                case "9":
                    myIniFile.WriteString("Setting", "临时阶段电流1", 参数.阶段电流1);
                    myIniFile.WriteString("Setting", "临时阶段电流2", 参数.阶段电流2);
                    myIniFile.WriteString("Setting", "临时阶段电流3", 参数.阶段电流3);
                    myIniFile.WriteString("Setting", "临时阶段电流4", 参数.阶段电流4);
                    myIniFile.WriteString("Setting", "临时阶段电流5", 参数.阶段电流5);
                    myIniFile.WriteString("Setting", "临时阶段电流6", 参数.阶段电流6);
                    myIniFile.WriteString("Setting", "临时阶段电流7", 参数.阶段电流7);
                    myIniFile.WriteString("Setting", "临时阶段电流8", 参数.阶段电流8);
                    myIniFile.WriteString("Setting", "临时阶段电流9", 参数.阶段电流9);
                    myIniFile.WriteString("Setting", "临时阶段电流10", "0");

                    myIniFile.WriteString("Setting", "临时阶段停留时长1", 参数.阶段停留时长1);
                    myIniFile.WriteString("Setting", "临时阶段停留时长2", 参数.阶段停留时长2);
                    myIniFile.WriteString("Setting", "临时阶段停留时长3", 参数.阶段停留时长3);
                    myIniFile.WriteString("Setting", "临时阶段停留时长4", 参数.阶段停留时长4);
                    myIniFile.WriteString("Setting", "临时阶段停留时长5", 参数.阶段停留时长5);
                    myIniFile.WriteString("Setting", "临时阶段停留时长6", 参数.阶段停留时长6);
                    myIniFile.WriteString("Setting", "临时阶段停留时长7", 参数.阶段停留时长7);
                    myIniFile.WriteString("Setting", "临时阶段停留时长8", 参数.阶段停留时长8);
                    myIniFile.WriteString("Setting", "临时阶段停留时长9", 参数.阶段停留时长9);
                    myIniFile.WriteString("Setting", "临时阶段停留时长10", "0");
                    break;
                case "10":
                    myIniFile.WriteString("Setting", "临时阶段电流1", 参数.阶段电流1);
                    myIniFile.WriteString("Setting", "临时阶段电流2", 参数.阶段电流2);
                    myIniFile.WriteString("Setting", "临时阶段电流3", 参数.阶段电流3);
                    myIniFile.WriteString("Setting", "临时阶段电流4", 参数.阶段电流4);
                    myIniFile.WriteString("Setting", "临时阶段电流5", 参数.阶段电流5);
                    myIniFile.WriteString("Setting", "临时阶段电流6", 参数.阶段电流6);
                    myIniFile.WriteString("Setting", "临时阶段电流7", 参数.阶段电流7);
                    myIniFile.WriteString("Setting", "临时阶段电流8", 参数.阶段电流8);
                    myIniFile.WriteString("Setting", "临时阶段电流9", 参数.阶段电流9);
                    myIniFile.WriteString("Setting", "临时阶段电流10", 参数.阶段电流10);

                    myIniFile.WriteString("Setting", "临时阶段停留时长1", 参数.阶段停留时长1);
                    myIniFile.WriteString("Setting", "临时阶段停留时长2", 参数.阶段停留时长2);
                    myIniFile.WriteString("Setting", "临时阶段停留时长3", 参数.阶段停留时长3);
                    myIniFile.WriteString("Setting", "临时阶段停留时长4", 参数.阶段停留时长4);
                    myIniFile.WriteString("Setting", "临时阶段停留时长5", 参数.阶段停留时长5);
                    myIniFile.WriteString("Setting", "临时阶段停留时长6", 参数.阶段停留时长6);
                    myIniFile.WriteString("Setting", "临时阶段停留时长7", 参数.阶段停留时长7);
                    myIniFile.WriteString("Setting", "临时阶段停留时长8", 参数.阶段停留时长8);
                    myIniFile.WriteString("Setting", "临时阶段停留时长9", 参数.阶段停留时长9);
                    myIniFile.WriteString("Setting", "临时阶段停留时长10", 参数.阶段停留时长10);
                    break;
                default:
                    MessageBox.Show("有效数组超限");
                    break;
            }

            myIniFile.WriteString("Setting", "临时有效阶段数", 参数.有效阶段数);
            myIniFile.WriteString("Setting", "临时阶梯加电状态", "true");
        }

        private void PreviewTextInput_vb2(Object sender, TextCompositionEventArgs e)
        {
            System.Windows.Controls.TextBox CTextBox = (System.Windows.Controls.TextBox)sender;
            PublicClass.tb_PreviewTextInput2(CTextBox, e);
        }

        private void 恢复(object sender, RoutedEventArgs e)
        {
            myIniFile.WriteString("Setting", "临时阶梯加电状态", "false");
        }
    }
}
